package com.bba.manager.as.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bba.manager.as.mapper.OldPartsMapper;
import com.bba.manager.as.service.IOldPartsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bba.manager.common.model.as.OldParts;
import com.bba.manager.common.model.as.VO.oldPartsQuery;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 汽车旧件表 服务实现类
 * </p>
 *
 * @author SYANDZZY
 * @since 2025-08-12
 */
@Service
public class OldPartsServiceImpl extends ServiceImpl<OldPartsMapper, OldParts> implements IOldPartsService {

    @Override
    public void updateOldPartsStatus(Long oldPartsCode, String status) {
        LambdaQueryChainWrapper<OldParts> query = new LambdaQueryChainWrapper<>(this.baseMapper);
        List<OldParts> list = query.eq(OldParts::getOldPartsNo, oldPartsCode).list();
        list.stream().map(e->{
            e.setStatus(status);
            return e;
        }).collect(Collectors.toList());
        this.updateBatchById(list);
    }

    @Override
    public IPage<OldParts> listByPage(oldPartsQuery query) {
        /**
         * todo: 分页查询
         * 1.如果有状态优先按状态查
         * 2.如果没有状态，按维修单号查
         * 3.按旧件名称查
         * 4.按旧件名称查
         */
        Page<OldParts> page=new Page<>(query.getPage(),query.getLimit());
        LambdaQueryChainWrapper<OldParts> queryWrapper = new LambdaQueryChainWrapper<>(this.baseMapper);
        if(query.getOldPartNo()!=null){
            queryWrapper.eq(OldParts::getOldPartsNo,query.getOldPartNo());
        } else if (query.getRepairNo()!=null){
            queryWrapper.eq(OldParts::getRepairNo,query.getRepairNo());
        } else if (query.getOldPartName()!=null){
            queryWrapper.eq(OldParts::getOldPartsName,query.getOldPartName());
        } else if (query.getOldPartStatus()!=null){
            queryWrapper.eq(OldParts::getStatus,query.getOldPartStatus());
        }
        return queryWrapper.page(page);
    }
}
